package com.wugenqiang.project.system.service.impl;

import com.wugenqiang.constant.ParamConstant;
import com.wugenqiang.project.system.dao.UserMapper;
import com.wugenqiang.project.system.pojo.User;
import com.wugenqiang.project.system.service.IUserService;
import com.wugenqiang.utils.encrypt.Md5Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Classname UserServiceImpl
 * @Description UserServiceImpl
 * @Date 2019/1/26 9:26
 * @Created by WuGenQiang//fighting!!
 */
@Service
public class UserServiceImpl implements IUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper userMapper;

    //新增用户信息
    @Override
    public String addUserInfo(User userInfo) {

        User user = new User();
        if (StringUtils.isNotBlank(userInfo.getUsername()) && StringUtils.isNotBlank(userInfo.getPassword())) {
            //用户密码加密
            String encodePwd = Md5Utils.MD5encode(userInfo.getUsername() + userInfo.getPassword());
            userInfo.setPassword(encodePwd);
            userMapper.addUserInfo(userInfo);
        }
        return ParamConstant.INSERT_SUCCESS;
    }

    //根据userId查询用户信息
    @Override
    public User queryUserByUserId(Integer userId) {
        User user = null;
        if (userId != null){
            user = this.userMapper.queryUserById(userId);
        }
        return user;
    }

    //根据username查询用户信息
    @Override
    public User queryUserByUsername(String username) {
        User user = null;
        if (username != null){
            user = this.userMapper.queryUserByUsername(username);
        }
        return user;
    }

    //更新用户信息
    @Override
    public String updateUserInfo(User user) {
        this.userMapper.updateUserInfo(user);
        return ParamConstant.UPDATE_SUCCESS;
    }

    //用户登录
    @Override
    public User login(String username, String password) {

        String pwd = Md5Utils.MD5encode(username+password);
        String pwd_inTable = userMapper.queryUserByUsername(username).getPassword();
        if(!pwd.equals(pwd_inTable)){
            return null;
        }
        return userMapper.queryUserByUsername(username);
    }

}
